package com.example.hyw.szysteam.view;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.example.hyw.szysteam.POJO.UserInfo;
import com.example.hyw.szysteam.R;
import com.example.hyw.szysteam.adapter.MyFragmentPagerAdapter;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;

import java.util.ArrayList;

/**
 * Created by HYW on 2018/6/8.
 */

public class ClassInfoActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ArrayList<Fragment> fragmentList=new ArrayList<>();
    private Fragment fragment1,fragment2;
    private MyFragmentPagerAdapter adapter;
    private QMUITopBar mTopBar;
    private String classId;
    private String className;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        Bundle bundle=getIntent().getExtras();
        classId=bundle.getString("classid");
        className=bundle.getString("classname");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classinfo);
        initView();
        initTopBar();
    }

    private void initView(){
        tabLayout=findViewById(R.id.tabs);
        viewPager=findViewById(R.id.viewpager);
        Bundle bundle1=new Bundle();
        bundle1.putString("classid",classId);
        fragment1=new ClassMemberFragment();
        fragment1.setArguments(bundle1);
        fragment2=new ClassWorkFragment();
        fragment2.setArguments(bundle1);
        fragmentList.add(fragment2);
        fragmentList.add(fragment1);
        adapter=new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(0).setText("作业");
        tabLayout.getTabAt(1).setText("成员");
    }
    private void initTopBar(){
        mTopBar=findViewById(R.id.topbar);
        mTopBar.addLeftImageButton(R.drawable.ic_left,R.id.left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mTopBar.setTitle(className).setTextColor(getResources().getColor(R.color.myWhite));
        mTopBar.addRightImageButton(R.drawable.ic_more,R.id.right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new QMUIBottomSheet.BottomListSheetBuilder(ClassInfoActivity.this)
                        .addItem("复制验证码")
                        .addItem("发布作业")
                        .setOnSheetItemClickListener(new QMUIBottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
                            @Override
                            public void onClick(QMUIBottomSheet dialog, View itemView, int position, String tag) {
                                dialog.dismiss();
                                switch (position){
                                    case 0:
                                        ClipboardManager cm=(ClipboardManager)getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE);
                                        cm.setText(classId);
                                        Toast.makeText(getApplicationContext(),"复制成功，快发给同学们吧！",Toast.LENGTH_LONG).show();
                                        break;
                                    case 1:
                                    default:
                                        if(UserInfo.getType()==1){
                                            Bundle bundle=new Bundle();
                                            bundle.putString("classid",classId);
                                            Intent intent=new Intent(ClassInfoActivity.this,CreateHomeWorkActivity.class);
                                            intent.putExtras(bundle);
                                            startActivity(intent);
                                        }else{
                                            Toast.makeText(ClassInfoActivity.this,"您不是管理员，不能发布作业！",Toast.LENGTH_SHORT).show();
                                        }
                                }
                            }
                        })
                        .build()
                        .show();
            }
        });
    }
}
